package br.com.amadeus.android.activities.home;


import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import br.com.amadeus.android.adapters.ImageAdapter;
import br.com.amadeus.android.adapters.SocialHistoryAdapter;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Config;
import br.com.amadeus.android.entities.SocialHistory;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;

public class HomeActivity extends Activity{
	
protected static final String CATEGORIA = "curso";
	
	// array com os ids das imagens
	public static final int SAIR = 1;
	
	TextView namePerson;
	ListView listview;
	ImageView photo;
	String notify;
	String name;
	FacadeAmadeusDroid facade = null;
	private NotificationManager nm;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.amadeus_screen_4);
		
		facade = FacadeAmadeusDroid.getInstance(this);
				
		Bundle gotBasket = getIntent().getExtras();
		
		
		try {
			String action = gotBasket.getString("reload");
			if (action != null && action.equals("reload")) {
				nm.cancel(R.string.app_name);
			}
		} catch (Exception e) {}
		
		this.name = "";
		Config config = FacadeAmadeusDroid.getInstance(this).getConfig();
		if (config != null) {
			if (config.getVersionSync() == 1) {
				this.notify = ConstantsAmadeusDroid.NOTIFY;
			}
			
			this.name = config.getUser_name();
		}
		
		if (notify != null) {
			
			if (notify.equals(ConstantsAmadeusDroid.NOTIFY)) {

				String tickerText = ConstantsAmadeusDroid.TICKER_TEXT_NOTIFICATION;

				CharSequence titulo = ConstantsAmadeusDroid.TITLE_NOTIFICATION;

				String mensagem = ConstantsAmadeusDroid.MESSAGE_NOTIFICATION;

				createNotification(this, tickerText, titulo, mensagem);
			}

		}
		
		
		
		GridView grid = (GridView) findViewById(R.id.GridView01);
		grid.setAdapter(new ImageAdapter(this));
		
		List<SocialHistory> listHistoric = FacadeAmadeusDroid.getInstance(this).getListSocialHistory();
		
		this.photo = (ImageView) findViewById(R.id.ivPerson);
		SocialHistoryAdapter sha = new SocialHistoryAdapter(this, listHistoric, this.photo);
		
		this.listview = (ListView) findViewById(R.id.lvSocialHistoric);
		this.listview.setAdapter(sha);
		
		this.namePerson = (TextView) findViewById(R.id.tvNamePerson);
		this.namePerson.setText(name);	
		
		/*try {
			final Bitmap imagem = BitmapFactory.decodeFile("/sdcard" + ConstantsAmadeusDroid.ROOT_ARCHIVES + "/image_user.png");
			if (imagem != null && !this.name.equals("")) {
				this.photo.setImageBitmap(imagem);
			}	
		} catch (Exception e) {
			Log.e(CATEGORIA, "Foto não encontrada.");
		}*/
		
		grid.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int posicao,long id) {
				switch (posicao) {
					case 0:
						//Toast.makeText(Screen4Activity.this, "CURSOS!", Toast.LENGTH_SHORT).show();
						//startActivity(new Intent("SCREEN5"));
						break;
					case 1:
						//Cursos
						startActivity(new Intent("COURSEON"));
						break;
					case 2:
						//Comunidade Portal do Software Público Brasileiro
						startActivity(new Intent("COMMUNITY"));
						break;
					case 3:
						//Configurações
						startActivity(new Intent("HOME_SETTINGS"));
						break;
					
				}
			}
		});
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		int opcao = Integer.parseInt(getPrefs.getString("listPref", "1"));
		if (opcao == ConstantsAmadeusDroid.MODO_OFFLINE) {
						
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setMessage("Para acessar todos os recursos da aplicação, verifique o acesso à Internet. Você está acessando no modo offline");
			dialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					
				}
			});
			dialog.show();
		}
		
		
		//video de libras
		ImageButton librasBt = (ImageButton)findViewById(R.id.botaoLibras3);
        librasBt.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent it = new Intent("ABRIR_LIBRAS");
				it.putExtra("video_libras", "Home.wmv");
				startActivity(it);
				
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		super.onCreateOptionsMenu(menu);
		// Adiciona quarto opções no menu
		
		MenuItem item = menu.add(0, SAIR, 0, "Sair");
		item.setIcon(R.drawable.sair);
		
		return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case SAIR:
			finish();
			return true;
		}
		return false;
	}
	
	private void createNotification(Context context,
			CharSequence mensagemBarraStatus, CharSequence titulo, CharSequence mensagem) {
		
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Intent it = new Intent("HOME");
		Bundle basket = new Bundle();
		basket.putString("reload", "reload");
		it.putExtras(basket);
		
		PendingIntent p = PendingIntent.getActivity(this, 0, it, 0);
		
		Notification n = new Notification(R.drawable.chat, mensagemBarraStatus, System.currentTimeMillis());
		
		n.setLatestEventInfo(this, titulo, mensagem, p);
		
		n.vibrate = new long[] { 100, 250, 100, 500 };
		
		nm.notify(R.string.app_name, n);
		
	}

}
